<?php
/**
 * Created by lzl
 * Date: 2021 2021/8/31
 * Time: 10:53
 */
namespace App\Http\Controllers\Admin;


use App\Forms\UploadForm;
use App\Models\Good;
use App\Models\Order;
use App\Models\User;

class PublicController extends Controller
{
    public function home()
    {
        return $this->view('home');
    }

    public function console()
    {
        //todo 后期有瓶颈时候这部分要放cache
        //用户总量  昨日增长量  近一周增长量
        $totalUser = User::count();

        $yesterdayUser = User::whereBetween('create_at', [date('Y-m-d:00:00:00', time()-86400) , date('Y-m-d:23:59:59', time()-86400)])->count();

        $weekUser = User::whereBetween('create_at', [date('Y-m-d:00:00:00', time()-86400 * 7) , date('Y-m-d:23:59:59', time())])->count();

        //来源share_free
        $shareFreeUser = User::where('source','share_free')->count();

        //来源常规
        $ruleUser = User::whereNull('source')->count();


        //订单总金额  昨日  近一周
        $totalAmount = Order::where('status', '>=',Order::DELIVER)->sum('pay_amount');

        $yesterdayAmount = Order::whereBetween('create_at', [date('Y-m-d:00:00:00', time()-86400) , date('Y-m-d:23:59:59', time()-86400)])
        ->where('status', '>=',Order::DELIVER)->sum('pay_amount');

        $weekAmount = Order::whereBetween('create_at', [date('Y-m-d:00:00:00', time()-86400 * 7) , date('Y-m-d:23:59:59', time())])
        ->where('status', '>=',Order::DELIVER)->sum('pay_amount');

        //商品总量
        $goods = Good::where('status',Good::ON_SALE)->count();

        //订单总量
        $order = Order::where('status','>=',Order::DELIVER)->count();


        return $this->view('console',compact(
            'totalUser','yesterdayUser','weekUser','shareFreeUser', 'ruleUser', 'totalAmount', 'yesterdayAmount',
            'weekAmount', 'goods', 'order'
        ));
    }


    /**
     * layui富文本上传图片
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function layUpload()
    {
        $rules = [
            'file' => 'required|file|max:102400',
        ];

        $this->validateInput($rules);

        return $this->successLayUpload(UploadForm::upload($this->validated['file']));
    }
}